import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { ECareer } from "../../../Message/Public/ECareer";
import { GameProxy } from "../mvc/GameProxy";
import { CareerUtil } from "../view/common/util/CareerUtil";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { GameController } from "../mvc/GameController";
import { SkillInfo } from "../model/SkillInfo";
import { TaskInfo } from "../model/TaskInfo";
import { GuideType } from "../rules/GuideType";
import { AIManager } from "../scene/ai/AIManager";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { LoaderManager } from "../../../com/gengine/resource/LoaderManager";
import { ImageInfo } from "../../../com/gengine/resource/info/ImageInfo";
import { WindowEvent } from "../../component/window/WindowEvent";
import { ICareerModule } from "../../../modules/interfaces/ICareerModule";
import { ICareerChangeModule } from "../../../modules/interfaces/ICareerChangeModule";
import { CareerChangeModule } from "../../../modules/CareerChangeModule";
import { CareerChangeIcon } from "../view/career/CareerChangeIcon";
import { IView } from "../../mvc/interfaces/IView";
import { CareerModule } from "../../../modules/CareerModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
type int = number;
//class CareerController
    
    export  class CareerController extends Controller
    {
       

        /* internal  */onCareerUpdateHandler(arg1: DataEvent): void
        {
            var loc1=0;
            if (cache.role.entityInfo.career == ECareer._ECareerNewPlayer || cache.role.entityInfo.career == ECareer._ECareerNo) 
            {
                loc1 = int(arg1.data);
                cache.guide.selectedCareer = true;
                GameProxy.career.updateCareerReq(loc1);
            }
            else if (cache.career.careerInfo) 
            {
                loc1 = CareerUtil.getGradeCareer(cache.role.entityInfo.career);
                GameProxy.career.updateCareerReq(loc1);
            }
            return;
        }

        /* internal  */onCareerTaskGetReq(arg1: DataEvent): void
        {
            if (!(cache.career.careerTaskId == 0) && cache.dialog.npcInfo) 
            {
                GameProxy.taskProxy.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, cache.career.careerTaskId);
            }
            return;
        }

        /* internal  */onRoleCareerUpdate(arg1: Object): void
        {
            if (cache.guide.selectedCareer) 
            {
                cache.guide.selectedCareerRes = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_UpdateCareer));
            }
            if (CareerUtil.isGradeCareer(cache.role.entityInfo.career)) 
            {
                cache.career.clearCareerInfo();
                if (this.isCareerChangeModuleShow) 
                {
                    this.careerChangeModule.hide();
                }
                GameController.skill.view.show();
                this.showCareerChangeIcon(false);
            }
            return;
        }

        /* internal  */onSkillUpdate(arg1: SkillInfo): void
        {
            cache.career.updateCareerTaskNeedLearnedSkill(arg1);
            if (this.isCareerChangeModuleShow) 
            {
                this.careerChangeModule.updateSkill();
            }
            return;
        }

        /* internal  */onTaskGetRes(arg1: Object): void
        {
            var loc1=null;
            if (arg1 && arg1.task) 
            {
                if (cache.dialog.npcInfo && cache.dialog.npcInfo.tnpc.npcId == arg1.npcId) 
                {
                    loc1 = arg1.task as TaskInfo;
                    if (loc1.sTask.guide & GuideType.GuideCareerChangeBegin) 
                    {
                        if (this.isCareerChangeModuleShow) 
                        {
                            this.careerChangeModule.hide();
                        }
                    }
                    else if (loc1.endCareer > 0) 
                    {
                        if (this.isCareerChangeModuleShow) 
                        {
                            this.careerChangeModule.hide();
                        }
                        AIManager.onAutoPathAIControl(cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectCareerChange));
                    }
                }
            }
            return;
        }

        /* internal  */onTaskEndRes(arg1: Object): void
        {
            var loc1=null;
            if (arg1 && arg1.task) 
            {
                if (cache.dialog.npcInfo && cache.dialog.npcInfo.tnpc.npcId == arg1.npcId) 
                {
                    loc1 = arg1.task as TaskInfo;
                    if (loc1.sTask.guide & GuideType.GuideCareerChangeEnd) 
                    {
                        if (this.isCareerChangeModuleShow) 
                        {
                            this.careerChangeModule.hide();
                        }
                        AIManager.onAutoPathAIControl(cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectCareerChange));
                    }
                }
            }
            return;
        }

        /* internal  */onCareerUpdateModuleOpenReq(arg1: DataEvent): void
        {
            if (this.isCareerChangeModuleShow) 
            {
                this.careerChangeModule.hide();
            }
            else 
            {
                if (CareerUtil.isGradeCareer(cache.role.entityInfo.career)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(27528));
                    return;
                }
                this.careerChangeModule.show();
                this.careerChangeModule.updateData(cache.career.careerInfo);
            }
            return;
        }

        /* internal  */onRoleLevelUpDateHandler(arg1: PlayerEvent): void
        {
            if (cache.role.entityInfo.level != 4) 
            {
                if (cache.role.entityInfo.level != 6) 
                {
                    if (cache.role.entityInfo.level >= 65 && !CareerUtil.isGradeCareer(cache.role.entityInfo.career)) 
                    {
                        this.showCareerChangeIcon(true);
                    }
                }
                else 
                {
                    this.loadCareerRes(2, 4);
                }
            }
            else 
            {
                this.loadCareerRes(0, 2);
            }
            if (this.isCareerChangeModuleShow) 
            {
                this.careerChangeModule.updateLevel();
            }
            return;
        }

        /* internal  */loadCareerRes(arg1: int, arg2: int): void
        {
            var loc2=0;
            var loc1=cache.career.careers;
            if (arg2 > loc1.length) 
            {
                return;
            }
            while (arg1 < arg2) 
            {
                loc2 = loc1[arg1];
                LoaderManager.instance.load("careerWeapon_" + loc2 + ".jpg", this.onCareerResLoaded);
                LoaderManager.instance.load("careerWeaponIcon_" + loc2 + ".png", this.onCareerResLoaded);
                LoaderManager.instance.load("careerNameIcon_" + loc2 + ".png", this.onCareerResLoaded);
                ++arg1;
            }
            return;
        }

        /* internal  */onCareerResLoaded(arg1: ImageInfo): void
        {
            return;
        }

        /* internal  */addListener(): void
        {
            return;
        }

        /* internal  */delListener(): void
        {
            return;
        }

        /* internal  */onWindowShowHandler(arg1: WindowEvent): void
        {
            (_view as ICareerModule).updateCareerList(cache.career.careers);
            return;
        }

        /* internal  */onCareerChangeShowHandler(arg1: WindowEvent): void
        {
            this.addListener();
            return;
        }

        /* internal  */onCareerChangeCloseHandler(arg1: WindowEvent): void
        {
            this.delListener();
            return;
        }

        public get careerChangeModule(): ICareerChangeModule
        {
            if (!this._careerChangeModule) 
            {
                this._careerChangeModule = new CareerChangeModule();
                this._careerChangeModule.addEventListener(WindowEvent.SHOW, this.onCareerChangeShowHandler);
                this._careerChangeModule.addEventListener(WindowEvent.CLOSE, this.onCareerChangeCloseHandler);
            }
            return this._careerChangeModule;
        }

        /* internal  */onLoginGameSuccessHandler(arg1: DataEvent): void
        {
            if (cache.role.entityInfo.level >= 65 && !CareerUtil.isGradeCareer(cache.role.entityInfo.career)) 
            {
                this.showCareerChangeIcon(true);
            }
            return;
        }

        /* internal  */showCareerChangeIcon(arg1: Boolean): void
        {
            if (arg1) 
            {
                if (!this._careerChangeIcon) 
                {
                    this._careerChangeIcon = new CareerChangeIcon();
                }
                if (this._careerChangeIcon.isHide) 
                {
                    this._careerChangeIcon.show();
                }
            }
            else if (this._careerChangeIcon && !this._careerChangeIcon.isHide) 
            {
                this._careerChangeIcon.hide();
            }
            return;
        }

        public get isCareerChangeModuleShow(): Boolean
        {
            return this._careerChangeModule && !this._careerChangeModule.isHide;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new CareerModule();
            loc1.addEventListener(WindowEvent.SHOW, this.onWindowShowHandler);
            return loc1;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.Career_Update, this.onCareerUpdateHandler);
            Dispatcher.addEventListener(EventName.CareerUpdataTaskGetReq, this.onCareerTaskGetReq);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerUpdate, this.onRoleCareerUpdate);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpDateHandler);
            Dispatcher.addEventListener(EventName.CareerUpdateModuleOpenReq, this.onCareerUpdateModuleOpenReq);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.onSkillUpdate);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onLoginGameSuccessHandler);
            return;
        }

        private /* var */_careerChangeModule: ICareerChangeModule;

        private /* var */_careerChangeIcon: CareerChangeIcon;
    }
